// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.network.packets.hit;

import java.io.IOException;
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import zombie.core.network.ByteBufferWriter;
import zombie.core.raknet.UdpConnection;
import zombie.debug.DebugLog;
import zombie.debug.LogSeverity;
import zombie.inventory.InventoryItem;
import zombie.inventory.InventoryItemFactory;
import zombie.network.packets.INetworkPacket;

public class PlayerItem extends Instance implements INetworkPacket {
    protected int itemId;
    protected InventoryItem item;

    public void set(InventoryItem _item) {
        super.set(_item.getRegistry_id());
        this.item = _item;
        this.itemId = this.item.getID();
    }

    @Override
    public void parse(ByteBuffer b, UdpConnection connection) {
        boolean _boolean = b.get() == 1;
        if (_boolean) {
            this.ID = b.getShort();
            b.get();

            try {
                this.item = InventoryItemFactory.CreateItem(this.ID);
                if (this.item != null) {
                    this.item.load(b, 195);
                }
            } catch (BufferUnderflowException | IOException iOException) {
                DebugLog.Multiplayer.printException(iOException, "Item load error", LogSeverity.Error);
                this.item = null;
            }
        } else {
            this.item = null;
        }
    }

    @Override
    public void write(ByteBufferWriter b) {
        if (this.item == null) {
            b.putByte((byte)0);
        } else {
            b.putByte((byte)1);

            try {
                this.item.save(b.bb, false);
            } catch (IOException iOException) {
                DebugLog.Multiplayer.printException(iOException, "Item write error", LogSeverity.Error);
            }
        }
    }

    @Override
    public boolean isConsistent() {
        return super.isConsistent() && this.item != null;
    }

    @Override
    public String getDescription() {
        return super.getDescription() + "\n\tItem [ Item=" + (this.item == null ? "?" : "\"" + this.item.getDisplayName() + "\"") + " ]";
    }

    public InventoryItem getItem() {
        return this.item;
    }
}
